package com.atguigu.flink.streamapi.trans;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;



/**
 * Created by Smexy on 2023/1/13
 *
 *  FlatMap可以替换 Map 和 Filter
 *          每个数字加10，再留下偶数
 */
public class Demo3_FlatMap
{
    public static void main(String[] args) {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        DataStreamSource<Integer> ds = env.fromElements(1, 2, 3, 4, 5, 6);

        //只留下返回true的数据
       /* ds
            .map(num -> num + 10)
            .filter(num -> num % 2 == 0)*/

       ds.flatMap((Integer num, Collector<Integer> out) -> {
               num = num + 10;
               if (num % 2 == 0){
                   out.collect(num);
               }
           })
           .returns(Types.INT)
          .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }




    }
}
